package zekezang.org;

import java.util.Timer;
import java.util.TimerTask;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;

import common.utils.activity.ActivityQueue;
import common.utils.activity.ActivityUtils;

import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class MyMapActivity extends com.baidu.mapapi.MapActivity {

	private RelativeLayout menu_layout = null;

	private common.utils.activity.MainMenu mainMenu = null;

	private MapView bmapView = null;
	private BMapManager bMapManager = null;
	private MapController mapController = null;

	private MKLocationManager locationManager = null;
	private MyLocationOverlay myLocationOverlay = null;
	private LocationListener locationListener = null;
	private GeoPoint geo = null;
	private Location location = null;

	private Handler locationHandler = null;
	private LoaclTask loaclTask = null;
	private Timer scheduler = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ActivityUtils.getInstance().setFullScreen(this);
		setContentView(R.layout.map_activity);

		ActivityQueue.activityList.add(this);

		menu_layout = (RelativeLayout) findViewById(R.id.menu_layout);
		menu_layout.setVisibility(View.VISIBLE);
		footerSetting();

		bMapManager = new BMapManager(getApplication());
		bMapManager.init("06476CDF891730240603BA79A0F1956E17BB9288", new MyGeneralListener());
		bMapManager.start();
		super.initMapActivity(bMapManager);

		bmapView = (MapView) findViewById(R.id.bmapView);
		// bmapView.setTraffic(true);
		bmapView.setBuiltInZoomControls(true);
		bmapView.setDrawOverlayWhenZooming(true);

		mapController = bmapView.getController();
		mapController.setZoom(14);
		
		locationManager = bMapManager.getLocationManager();
		locationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
		myLocationOverlay = new MyLocationOverlay(getApplicationContext(), bmapView);
		myLocationOverlay.enableMyLocation(); // 启用定位
		myLocationOverlay.enableCompass();    // 启用指南针
		

		locationHandler = new Handler(Looper.myLooper()) {
			public void handleMessage(android.os.Message msg) {
				locationPoint();
			}
		};

		locationListener = new LocationListener() {
			@Override
			public void onLocationChanged(Location arg0) {
				locationPoint();
			}
		};

		loaclTask = new LoaclTask(locationHandler);
		scheduler = new Timer();
		scheduler.schedule(loaclTask, Long.valueOf("1000").longValue(), Long.valueOf("5000").longValue());
	}

	private void locationPoint() {
		if (locationManager != null) {
			location = locationManager.getLocationInfo();
			if(location != null){
				geo = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
				mapController.animateTo(geo);
			}
		}
	}
	
	private void registerMapListener() {
		
		bmapView.getOverlays().clear();
		bmapView.getOverlays().add(myLocationOverlay);
		
		locationManager.removeUpdates(locationListener);
		locationManager.requestLocationUpdates(locationListener);
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		initMapHeight();
		registerMapListener();

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		locationManager.removeUpdates(locationListener);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		scheduler.cancel();
	}

	private void initMapHeight() {
		int[] p = ActivityUtils.getInstance().getWidthHight(this);
		RelativeLayout.LayoutParams params = (LayoutParams) bmapView.getLayoutParams();
		params.height = p[1] - 30 - 30;
	}

	private void footerSetting() {
		mainMenu = common.utils.activity.MainMenu.getInstance(this, this, menu_layout, null, "147", null);
		// menu向上键
		ImageButton menu_up_button = (ImageButton) menu_layout.findViewById(R.id.menu_up_button);
		menu_up_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnKeyDown();
			}
		});
		// menu主页
		final ImageButton menu_home_button = (ImageButton) menu_layout.findViewById(R.id.menu_home_button);
		menu_home_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				ActivityUtils.getInstance().to_home_Transition(MyMapActivity.this, getApplicationContext(),
						MainActivity.class);
			}
		});
		// menu后退
		final ImageButton menu_back_button = (ImageButton) menu_layout.findViewById(R.id.menu_back_button);
		menu_back_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				ActivityUtils.getInstance()
						.back_leftOUT_rightIN_Transition(MyMapActivity.this, getApplicationContext());
			}
		});
		// menu前进
		final ImageButton menu_go_button = (ImageButton) menu_layout.findViewById(R.id.menu_go_button);
		menu_go_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				menu_go_button.setEnabled(false);
			}
		});
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	class MyGeneralListener implements MKGeneralListener {
		@Override
		public void onGetNetworkState(int iError) {}

		@Override
		public void onGetPermissionState(int iError) {}
	}

}

class LoaclTask extends TimerTask {
	private Handler handler = null;

	public LoaclTask(Handler handler) {
		this.handler = handler;
	}

	@Override
	public void run() {
		handler.sendEmptyMessage(0);
	}

}
